<?php

namespace Morrow;

class PageController extends DefaultController {
	public function run() {




		$code = $this->input->get('id');

		if(!$code){
			$this -> url -> redirect('home');
		}
		//var_dump($uid);
		$this->load('Db', $this->config->get('db'));	
		$sql_webapps=$this->db->result('
			SELECT *
			FROM webapps
			WHERE code = ?'
			, $code);
		//var_dump($sql["RESULT"]);

		$this->view->setContent($sql_webapps, 'data');

		$jiami = $sql_webapps["RESULT"][0]["jiami"];

		$sql_users=$this->db->result('
			SELECT *
			FROM users
			WHERE uid = ?'
			, $sql_webapps['RESULT'][0]['uid']);

		if($sql_users['RESULT'][0]['username']==""){

			$username = "匿名";
			
		}
		else{
			$username=$sql_users['RESULT'][0]['username'];

		}
		
		$this->view->setContent($username, 'username');
		//$this->view->setContent($sql_users['RESULT'][0], 'user');

		$user = $sql_users['RESULT'][0];

		$name = $user["name"];
		$org = $user["org"];
		$adresse = $user["adresse"];
		$zip = $user["zip"];
		$city = $user["city"];
		$country = $user["country"];
		$cardemail = $user["cardemail"];
		$tel = $user["tel"];
		$mobile = $user["mobile"];

		if($name){
			$new_name="<strong>".$name."</strong><br/>";
		}
		else{$new_name="";}

		if($org){
			$new_org=$org."<br/>";
		}
		else{$new_org="";}

		if($adresse){
			$new_adresse=$adresse."<br/>";
		}
		else{$new_adresse="";}

		if($zip){
			$new_zip=$zip;
		}
		else{$new_zip="";}

		if($city){
			$new_city=$city."<br/>";
		}
		else{$new_city="";}

		if($country){
			$new_country=$country."<br/>";
		}
		else{$new_country="";}

		if($cardemail){
			$new_cardemail="<br/><a href='mailto:".$cardemail."'><img src='main/img/icon/glyphicons_010_envelope.png'></a>&nbsp;&nbsp;";
		}
		else{$new_cardemail="<br/>";}

		if($tel){
			$new_tel=" <a href='tel:".$tel."'><img src='main/img/icon/glyphicons_020_home.png'></a>&nbsp;&nbsp;";
		}
		else{$new_tel="";}

		if($mobile){
			$new_mobile="<a href='tel:".$mobile."'><img src='main/img/icon/glyphicons_163_iphone.png'></a>&nbsp;&nbsp;";
		}
		else{$new_mobile="";}



		$new_user = $new_name." ".$new_org." ".$new_adresse." ".$new_zip." ".$new_city." ".$new_country." ".$new_cardemail." ".$new_tel." ".$new_mobile;

		@$seewebapp = $this->session->get("seewebapp");
		$wid = $sql_webapps["RESULT"][0]["wid"];


		if($seewebapp == $wid){
			$this->view->setContent($new_user, 'user');
			$export_button="<div class='hidden-desktop' style='margin-bottom:20px; text-decoration:none;'><a href='vcard/".$sql_webapps['RESULT'][0]['wid']."' style='text-decoration:none;'><button class='btn btn-large btn-block btn-info'>导出联系方式</button></a></div>";
			$this->view->setContent($export_button, 'export_button');
			$this->session->delete("seewebapp");

		}
		else{
			if($jiami == 0){

				$this->view->setContent($new_user, 'user');
				$export_button="<div class='hidden-desktop' style='margin-bottom:20px; text-decoration:none;'><a href='vcard/".$sql_webapps['RESULT'][0]['wid']."' style='text-decoration:none;'><button class='btn btn-large btn-block btn-info'>导出联系方式</button></a></div>";
				$this->view->setContent($export_button, 'export_button');
			}
			else{
				$linksstring="jiemi/".$sql_webapps['RESULT'][0]['wid'];
				$this -> url -> redirect($linksstring);				
			}
		}

	}	
}